package com.itboye.ihomebank.activity.finance;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.CurrentDaiKuanBean;
import com.itboye.ihomebank.bean.HuanKuansBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.web.WebActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

import static com.itboye.ihomebank.util.TimesUtils.getStringToDateFive;
import static com.itboye.ihomebank.util.TimesUtils.getStringToDateFour;

/**
 * 还款
 */
public class JieKuanRecordActivity extends BaseOtherActivity implements Observer {
    ListView list_record;
    TextView txt_title;
    int type = 0;
    RelativeLayout nodata;
    private ArrayList<HashMap<String, Object>> listItems = new ArrayList<>();
    ImageView img_back;
    JinRongPresenter jinRongPresenter;
    String uid;
    int currentPage = 1;
    Button btn_jiekuan;
    private CurrentDaiKuanBean ar;
    PtrFrameLayout ptr;
    int itemId;
    MyApplcation myApp;
    public int totalCount;
    private HuanKuansBean huanKUanDetailBean;

    @Override
    protected int layout_id() {
        return R.layout.activity_jie_kuan_record;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        myApp = (MyApplcation) getApplication();
        myApp.jieKuanRecordUI = this;
        BasePtr.setPagedPtrStyle(ptr);
        ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                currentPage++;
                refreshList();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                currentPage = 1;
                refreshList();
            }
        });
        jinRongPresenter = new JinRongPresenter(this);
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        type = getIntent().getIntExtra("type", 0);
        itemId = getIntent().getIntExtra("item_id", 0);

        txt_title.setText(type == 1 ? "还款" : "借款记录");

        list_record.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (type == 1) {
                    Intent intent = new Intent(JieKuanRecordActivity.this, HuanKuanDetailActivity.class);
                    intent.putExtra("model", huanKUanDetailBean);
                    intent.putExtra("item_id", (int) listItems.get(position).get("item_id"));
                    intent.putExtra("position", (int) listItems.get(position).get("position"));
                    startActivity(intent);
                } else {

                }
            }
        });
    }

    private void initList(ArrayList<HashMap<String, Object>> listItems) {
        if (listItems.size() <= 0) {
            nodata.setVisibility(View.VISIBLE);
        } else {
            nodata.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshList();
    }

    public void refreshList(){
        if (type == 0) {
            jinRongPresenter.jiekuanHistory(uid, currentPage + "", "10");
        } else {
            jinRongPresenter.huanKuanDetail(uid, itemId + "");
        }

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_jiekuan:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra("url", NetPublicConstant.WEB_URL02+"/loan/instruction");
                startActivity(intent);
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ptr.refreshComplete();
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (!entity.getCode().equals("0")) {
                ByAlert.alert(entity.getMsg());
                initList(listItems);
                return;
            }
            if (entity.getEventType() == JinRongPresenter.jiekuanHistory_success) {
                ar = (CurrentDaiKuanBean) entity.getData();
                if (ar != null) {
                    if (ar.getList().size() > 0) {
                        listItems = new ArrayList<HashMap<String, Object>>();
                        for (int i = 0; i < ar.getList().size(); i++) {
                            HashMap<String, Object> map = new HashMap<String, Object>();
                            map.put("date", getStringToDateFive(ar.getList().get(i).getLoan_time()));
                            map.put("money", ar.getList().get(i).getTotal_money() / 100 + "");
                            listItems.add(map);
                        }
                        list_record.setAdapter(new SimpleAdapter(this, listItems, R.layout.item_record_huankuan, new String[]{"date", "money"}, new int[]{R.id.txt_date, R.id.money1}));
                    }
                    initList(listItems);
                }
            } else if (entity.getEventType() == JinRongPresenter.jiekuanHistory_fail) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == JinRongPresenter.huanKuanDetail_success) {
                huanKUanDetailBean = (HuanKuansBean) entity.getData();
                listItems = new ArrayList<HashMap<String, Object>>();
                for (int i = 0; i < huanKUanDetailBean.getPlan().size(); i++) {
                    if (huanKUanDetailBean.getPlan().get(i).getHas_pay() == 1) {
                        continue;
                    }
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("date", getStringToDateFour(huanKUanDetailBean.getPlan().get(i).getCan_pay_end() + ""));
                    map.put("money", DoubleUtil.doubleTransform(huanKUanDetailBean.getPlan().get(i).getTotal()));
                    String lixia="";
                    if (huanKUanDetailBean.getPlan().get(i).getOverdue_money().equals("")){
                        lixia="含利息" + DoubleUtil.doubleTransform(huanKUanDetailBean.getPlan().get(i).getAccrual()+"");
                    }else{
                        lixia="含利息" + (DoubleUtil.doubleTransform(huanKUanDetailBean.getPlan().get(i).getAccrual()+"")+"  滞纳金" + (DoubleUtil.doubleTransform(huanKUanDetailBean.getPlan().get(i).getOverdue_money())));
                    }
                    map.put("lixi", lixia);
                    map.put("arrow", R.drawable.jiantou_gray);
                    map.put("item_id", huanKUanDetailBean.getPlan().get(i).getItem());
                    map.put("position", i);
                    listItems.add(map);
                }
                list_record.setAdapter(new SimpleAdapter(this, listItems, R.layout.item_record_huankuan, new String[]{"date", "money",
                        "lixi", "arrow"}, new int[]{R.id.money1, R.id.txt_money, R.id.txt_lixi, R.id.arrow}));
                initList(listItems);
            } else if (entity.getEventType() == JinRongPresenter.huanKuanDetail_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        totalCount=listItems.size();
    }
}
